package com.batchengine.task;

import com.batchengine.DataAdapter;
import com.batchengine.Definitions;
import static com.batchengine.Definitions.DataAdapter.FILE;
import com.batchengine.util.EnvUtil;
import com.batchengine.util.LoggingFactory;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import static java.lang.Long.parseLong;
import static java.lang.String.format;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by  solutions
 * Author: sunil.incubator@yahoo.com
 * Date: Aug 8, 2009
 */
public abstract class TaskParser {

    private static final Logger logger = LoggingFactory.createLogger();
    protected Map<Long, TaskInfo> taskMap = null;

    private TaskParser() {
    }

    public static TaskParser getTaskDataParser(DataAdapter adapter) {
        assert null != adapter;
        return new FileParser();
    }

    public abstract Definitions.DataAdapter adapterType();

    public abstract Map<Long, TaskInfo> getTaskConfigurationMap();

    private static class FileParser extends TaskParser {

        @Override
        public Definitions.DataAdapter adapterType() {
            return FILE;
        }

        public Map<Long, TaskInfo> getTaskConfigurationMap() {

            if (null != taskMap) {
                return taskMap;
            }
            taskMap = new TreeMap<Long, TaskInfo>();

            try {


                BufferedReader reader = null;
                String line;
                reader = new BufferedReader(new FileReader(String.format("%s/task.conf", EnvUtil.getConfFolderLocation())));
                while (null != (line = reader.readLine())) {

                    if (line.startsWith("#") || 0 == line.trim().length()) {
                        continue;
                    }
                    String[] strings = line.split(",");
                    if (5 != strings.length || !line.endsWith(";")) {
                        logger.error(format("#not able to parse task config line %s", line));
                        continue;
                    }

                    try {

                        long taskId = parseLong(strings[0]);
                        TaskInfo context = new TaskInfo(taskId);
                        context.setTaskId(taskId);
                        context.setTaskType(Definitions.TaskType.getType(Integer.parseInt(strings[1])));
                        context.setClazzName(strings[2]);
                        context.setSelctorClazz(strings[3]);
                        if (null != strings[4] && 1 < strings[4].trim().length()) {
                            context.setSignalClazz(strings[4].substring(0, strings[4].indexOf(";")));
                        }
                        taskMap.put(taskId, context);
                    } catch (Exception e) {
                        logger.error(format("#not able to parse task config line %s", line), e);
                    }
                }
            } catch (Exception e) {
                logger.error(format("error@TaskParser#%s#getTaskConfigurationMap", adapterType()));
            }
            return taskMap;
        }
    }

    public static void main(String[] args) {

        TaskParser parser = TaskParser.getTaskDataParser(DataAdapter.FILE);
        Map<Long, TaskInfo> map = parser.getTaskConfigurationMap();
        for (Long o : map.keySet()) {
            System.out.println(map.get(o));
        }
    }
}
